﻿import os
import clr
import System

clr.AddReference("DyUi.Base")
import DyUi.Base.Constants.SubSystems
from DyUi.Base.Constants.SubSystems import *

class Folder(object):    
    FolderLabel = ""
    FullPath = ""
    Folders = []

class Nav(object): 
    
    FolderList = []
    
    _control = None
    
    def __init__(self, control, settings):        
        self._control = control
        
    def prepare(self):
        print "In Prepare"        
        self._control.MessageService.Subscribe(lambda a, b: a == AppletSubSystem.Message_Notify_AppletDirs_Update, lambda a, b, c: self.Event_UpdateAppletDirecotries(b))
        self._control.MessageService.Publish(AppletSubSystem.Message_Request_AppletDirs_Update, "");    
                
    def Event_UpdateAppletDirecotries(self, direcotries):
        print "Got Direcotires"
        
    def UpdateFolderList(self):
        if hasattr(settings, "FolderList"): 
    
            def getDyuiFolders(dir): 
                folders = []
                for child in os.listdir(dir):
                    folder = Folder()
                    folder.FullPath = child
                    folder.FolderLabel = child
                    folder.Folders = []       
                    folders.append(folder)                                 
                return folders
            
            for folderPath in settings.FolderList:
                folder = Folder()
                folder.FullPath = folderPath
                folder.FolderLabel = os.path.split(folderPath)[1]
                folder.Folders = getDyuiFolders(folderPath)
                self.FolderList.append(folder)
    
    def MenuItem_CreateInstance_Click(self, sender, args) :
        self._control.EventService.Publish("Controller.CreateAppletInstance", "HelloWorld");
